home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12816 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Operator overload problem!
  5. Date: Thu, 21 Mar 1996 16:07:26 -0500
  6. Organization: Datalytics, Inc
  7. Message-ID: <3151C50E.6A3@datalytics.com>
  8. References: <DoIn1z.Gou@latcs1.lat.oz.au>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Gregary J Boyles wrote:
  16. > I am trying to overload the << but I get the syntax errors : friends must be functions or
  17. > classes, 'ostream' cannot start a parameter declaration, 'Address::operator <<(int &,Address &)'
  18. > must be declared at the first set of astericks and declaration syntax error at the second set.
  19. > What the #$%& is it on about? I copied the operator overload function directly out of a program
  20. > which compiles and runs so why all of a sudden won't the bloody compiler accept it?
  21. > [snip]
  22. >             * friend ostream& operator <<(ostream& OutPutStream,Address& AnAddress); *
  23.  
  24. The compiler is simply not explaining itself well.  The error 
  25. message sounds like one from Microsoft Visual C++.  The problem 
  26. is that you haven't declared ostream at the time you make 
  27. reference to it.
  28.  
  29. Neither your header nor your .cpp file #include's iostream.h.  
  30. Ideally, you would forward declare ostream in the header, like 
  31. this:
  32.  
  33. class ostream;
  34.  
  35. class Address
  36. {
  37. ...
  38.  
  39. and then #include iostream.h in your .cpp file.
  40.  
  41. This will incompletely, but sufficiently, declare ostream in 
  42. your header, and will completely declare it in your .cpp file.  
  43. The advantage is one of compilation speed.  Files #include'ing 
  44. address.h won't pay the penalty of #include'ing iostream.h too.
  45.  
  46. -- 
  47. Robert Stewart        | My opinions are usually my own.
  48. Datalytics, Inc.    | stew@datalytics.com
  49.